var _VIEW_PATH = '../app_module/diy/views/';

var M = require('../models');

var productM = M.productM;
var cartM = M.cartM;
module.exports = {
    list : function(req, res){
        res.render(_VIEW_PATH + 'product/list',{title:'自提年货产品列表'});
    },
    detail:function(req,res){
        var uid = req.AV.user.id;
        var id = req.params.id;
        productM.get(id).then(function(data){
            cartM.get(uid,id).then(function(list){
                var cartProduct = {};
                if(!list){
                    cartProduct = {number:1};
                }else{
                    if(list.length>0){
                        cartProduct = list[0].toJSON();
                    }else{
                        cartProduct = {number:1};
                    }
                }
                res.render(_VIEW_PATH + 'product/detail',{title:data.name,product:data,cartProduct:cartProduct});
            })

        });
        //res.send('hello '+ req.params.id);
    },
    getPrice:function(req,res){
        var id = req.params.id;
        var number = req.params.number;
        productM.get(id).then(function(data){
            //console.log(data);
            //判断数量对应的价格
            var arr = data.prices;
            var len = arr.length;
            for(var i = len-1; i>=0 ; i--){
                if(number>=arr[i].point){
                    res.json({"code":0,"price": arr[i].grprice});
                }
            }
        });
    }
}